package com.ruoyi.project.lesson.block.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.block.domain.LessonBlock;
import com.ruoyi.project.lesson.block.service.ILessonBlockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程板块Controller
 *
 * @author shixiangwen
 * @date 2020-12-07
 */
@Controller
@RequestMapping("/lesson/block")
public class LessonBlockController extends BaseController {

    private String prefix = "lesson/block";

    @Autowired
    private ILessonBlockService lessonBlockService;

    @RequiresPermissions("lesson:block:view")
    @GetMapping()
    public String block() {
        return prefix + "/block";
    }

    /**
     * 查询课程板块列表
     */
    @RequiresPermissions("lesson:block:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LessonBlock lessonBlock) {
        startPage();
        List<LessonBlock> list = lessonBlockService.selectLessonBlockList(lessonBlock);
        return getDataTable(list);
    }

    /**
     * 导出课程板块列表
     */
    @RequiresPermissions("lesson:block:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LessonBlock lessonBlock) {
        List<LessonBlock> list = lessonBlockService.selectLessonBlockList(lessonBlock);
        ExcelUtil<LessonBlock> util = new ExcelUtil<LessonBlock>(LessonBlock.class);
        return util.exportExcel(list, "block");
    }

    /**
     * 新增课程板块
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.addAttribute("sort", lessonBlockService.getMaxSort());
        return prefix + "/add";
    }

    /**
     * 新增保存课程板块
     */
    @RequiresPermissions("lesson:block:add")
    @Log(title = "课程板块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LessonBlock lessonBlock) {
        return toAjax(lessonBlockService.insertLessonBlock(lessonBlock));
    }

    /**
     * 修改课程板块
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LessonBlock lessonBlock = lessonBlockService.selectLessonBlockById(id);
        mmap.put("lessonBlock", lessonBlock);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程板块
     */
    @RequiresPermissions("lesson:block:edit")
    @Log(title = "课程板块", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LessonBlock lessonBlock) {
        return toAjax(lessonBlockService.updateLessonBlock(lessonBlock));
    }

    /**
     * 删除课程板块
     */
    @RequiresPermissions("lesson:block:remove")
    @Log(title = "课程板块", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lessonBlockService.deleteLessonBlockByIds(ids));
    }
}
